<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/29
 * Time: 10:50
 */
namespace app\admin\controller;
use think\Page;
class Admin extends Base{

    public function index(){
        $model=db('admin');
        $p=I("get.p",1,'intval');
        $p=$p>0?:1;
        $pageSize=10;
        $model->_as('a')->join("admin_role ar","a.role_id=ar.id");
        $totalRow=$model->count();
        $list=$model->_as('a')->field('a.id,a.role_id,a.username,ar.role_name,ar.auth,a.add_time')->join("admin_role ar","a.role_id=ar.id")->paginate(2,$totalRow);
        $this->assign('list',$list);
        $this->assign('page',$list->render());
        return $this->fetch('index');
    }

    public function add(){
        if($_POST){
            $role_id=I("post.role_id",0,'intval');
            $name=I("post.username",'','trim');
            $pwd=I("post.password",'','trim');
            $pwd1=I('post.repassword','','trim');
            $role=db('admin_role')->where(['id'=>$role_id])->find();
            $role || die(json_error('权限组选择错误'));
            $name || die(json_error('账号不能为空'));
            if(!preg_match('/^[0-9a-zA-Z]+$/',$name)){
                die(json_error('账号只能是数字与大小写字母组成'));
            }
            if(strlen($name)<5 || strlen($name)>20){
                die(json_error('账号长度只能5到20位'));
            }
            $num=db('admin')->where(['username'=>$name])->count();
            $num>0 && die(json_error('该账号已存在'));
            $pwd || die(json_error('密码不能为空'));
            if(!preg_match('/^[0-9a-zA-Z]+$/',$pwd)){
                die(json_error('密码只能是数字与大小写字母组成'));
            }
            if(strlen($pwd)<5 || strlen($pwd)>20){
                die(json_error('密码长度只能5到20位'));
            }
            $pwd1  || die(json_error('请输入确认密码'));
            $pwd==$pwd1 || die(json_error('两次密码不一致'));

            $salt=get_salt();
            $data=[
                'role_id'=>$role_id,
                'username'=>$name,
                'password'=>md5_pwd($pwd,$salt),
                'add_time'=>time(),
                'add_ip'=>get_client_ip(),
                'salt'=>$salt
            ];
            $res=db('admin')->insert($data);
            $res || die(json_error('添加失败'));
            die(json_success('添加成功',['url'=>U('admin/index')]));
        }
        $model=db('admin_role');
        $list=$model->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function edit(){
        $model=db('admin');
        if($_POST){
            $role_id=I("post.role_id",0,'intval');
            $name=I("post.username",'','trim');
            $pwd=I("post.password",'','trim');
            $pwd1=I('post.repassword','','trim');
            $id=I("post.id",0,'intval');
            $role_n=db('admin_role')->where(['id'=>$role_id])->count();
            $role_n>0 || die(json_error('权限组选择错误'));
            $name || die(json_error('账号不能为空'));
            if(!preg_match('/^[0-9a-zA-Z]+$/',$name)){
                die(json_error('账号只能是数字与大小写字母组成'));
            }
            if(strlen($name)<5 || strlen($name)>20){
                die(json_error('账号长度只能5到20位'));
            }

            $admin=db('admin')->where(['id'=>$id])->find();
            $admin || die(json_error('参数错误'));
            if($admin['username'] !=$name){
                $n=$model->where(['username'=>$name])->count();
                $n>0 && die(json_error('该账号已存在'));
            }
            $pwd || die(json_error('密码不能为空'));
            if(!preg_match('/^[0-9a-zA-Z]+$/',$pwd)){
                die(json_error('密码只能是数字与大小写字母组成'));
            }
            if(strlen($pwd)<5 || strlen($pwd)>20){
                die(json_error('密码长度只能5到20位'));
            }
            $pwd1  || die(json_error('请输入确认密码'));
            $pwd==$pwd1 || die(json_error('两次密码不一致'));
            $salt=get_salt();
            $data=[
                'role_id'=>$role_id,
                'username'=>$name,
                'password'=>md5_pwd($pwd,$salt),
                'update_time'=>time(),
                'update_ip'=>get_client_ip(),
                'salt'=>$salt
            ];
            $res=db('admin')->where(['id'=>$id])->update($data);
            $res || die(json_error('修改失败'));
            die(json_success('修改成功',['url'=>U('admin/index')]));
        }
        $id=I("param.id",0,'intval');
        $info=$model->where(['id'=>$id])->find();
        $info || die(json_error('数据错误'));
        $list=db('admin_role')->select();
        $this->assign('list',$list);
        $this->assign('info',$info);
        return $this->fetch('edit');
    }

    public function ajaxDelete(){
        $id=I("get.id",0,'intval');
        $model=db('admin');
        $menu=$model->where(['id'=>$id])->find();
        $menu || die(json_error('参数错误'));
        $res=$model->where(['id'=>$id])->delete();
        $res || die(json_error('删除失败'));
        die(json_success('成功'));
    }

}